package com.hyt.it.ogt.ks.controller;


import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.hyt.it.ogt.ks.config.KsBizRunTimeException;
import com.hyt.it.ogt.ks.model.entity.*;
import com.hyt.loginfo.model.User;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.mapper.PaperTopicMapper;
import com.hyt.it.ogt.ks.model.vo.AnswerProgressVO;
import com.hyt.it.ogt.ks.service.IAnswerProgressService;
import com.hyt.it.ogt.ks.service.ICandidateAnswerService;
import com.hyt.it.ogt.ks.service.ICandidatePaperService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

/**
 * @author liuhenghuo
 */
@RestController
@RequestMapping("/ks/answer/progress/")
@Api(tags = "16.答题进度", value = "答题进度")
@ApiSort(16)
@Slf4j
public class AnswerProgressController {
    @Resource
    private IAnswerProgressService iAnswerProgressService;


    @ApiOperation(value = "16.1 获取当前题进度", response = AnswerProgressVO.class)
    @ApiOperationSort(1)
    @GetMapping("getCurrentItem")
    public ApiResponse<AnswerProgressVO> getCurrentItem(@RequestParam String examId,
                                                        @RequestParam String candidateId,
                                                        @RequestParam Integer paperSort) {
        AnswerProgressVO answerProgressVO = new AnswerProgressVO();
        try {
            log.info("获取当前题进度:{}、{}、{}", examId, candidateId, paperSort);
            answerProgressVO = iAnswerProgressService.getCurrentItem(examId,candidateId,paperSort);
        } catch (Exception e) {
            log.error("", e);
        }
        return ApiResponse.<AnswerProgressVO>builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                .data(answerProgressVO).build();
    }

    @ApiOperation(value = "16.2 保存答题进度")
    @ApiOperationSort(2)
    @PostMapping("saveCurrentItem")
    public ApiResponse saveCurrentItem(@RequestBody @Valid AnswerProgress answerProgress) {
        try {
            log.info("保存答题进度:{}", JSON.toJSONString(answerProgress));
            //iAnswerProgressService.saveItem(answerProgress);
            iAnswerProgressService.saveCurrentItem(answerProgress);
        } catch (Exception e) {
            log.error("", e);
        }
        return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode()).build();
    }

    @ApiOperation(value = "16.3 定时保存答题进度")
    @ApiOperationSort(3)
    @PostMapping("timingSaveItem")
    public ApiResponse<Map<String,Boolean>> timingSaveItem(@RequestBody @Valid AnswerProgress answerProgress) {
        try {
            log.info("定时保存答题进度:{}", JSON.toJSONString(answerProgress));
            if(Objects.isNull(answerProgress.getAnswerTime())){
                log.error("定时保存答题进度:{},考生用时为空", JSON.toJSONString(answerProgress));
                return ApiResponse.<Map<String,Boolean>>builder().code(BizStatusCode.TIMING_SAVE_ERROR.getCode()).build();
            }
            if(Objects.isNull(answerProgress.getItemId())){
                log.error("定时保存答题进度:{},考生用时试题为空", JSON.toJSONString(answerProgress));
                return ApiResponse.<Map<String,Boolean>>builder().code(BizStatusCode.TIMING_SAVE_ITEMID_ERROR.getCode()).build();
            }
            //iAnswerProgressService.saveItem(answerProgress);
            return ApiResponse.<Map<String,Boolean>>builder().data(iAnswerProgressService.timingSaveItem(answerProgress)).code(BizStatusCode.KS_SUCCESS_OK.getCode()).build();
        } catch (Exception e) {
            log.error("", e);
        }
        return ApiResponse.<Map<String,Boolean>>builder().code(BizStatusCode.KS_SUCCESS_OK.getCode()).build();
    }
}
